March 12, 2015

What is ggvis?

  • A data visualization package in R
  • Builds graphics declaratively using layers in the spirit of ggplot2
  • Produces graphics rendered in a web browser using Vega, a visualization grammar built on top of D3
  • Produces interactive graphics leveraging the power of Shiny

ggplot2 vs ggvis

ggplot(mtcars, aes(x=wt, y=mpg))+
  geom_point()

plot of chunk unnamed-chunk-1

ggplot2 vs ggvis

mtcars %>% 
  ggvis(x= ~wt, y= ~mpg) %>% 
  layer_points()

ggplot2 vs ggvis

mtcars %>% 
  ggvis(x= ~wt, y= ~mpg) %>% 
  layer_points() %>% layer_smooths()

ggplot2 vs ggvis

ggplot2 ggvis
geom layer (names end with "s")
stat compute
aes() props()

  • ggvis uses a function interface, so chaining is done using %>% rather than +
  • ggvis uses x = ~wt instead of x = wt (will come back to this)
  • Using ggvis() without any layers is akin to qplot

ggvis semantics

Basic template

<data>  %>% 
  ggvis(~<x property>,~<y property>, 
        fill = ~<fill property>, ...) %>% 
  layer_<marks>()`

<marks> can be arcs, bars, densities, freqpolys, histograms, images, lines, paths, model_predictions, points, rects, ribbons, smooths, text

Components of a ggvis visualization

faithful %>% 
    ggvis(~waiting, ~eruptions, fill := "red") %>% 
    layer_points() %>% 
    add_axis("y", title = "Duration of eruption (m)", 
             values = c(2, 3, 4, 5), subdivide = 9) %>% 
    add_axis("x", title = "Time since previous eruption (m)")

  • Data
  • A coordinate system
  • Marks
  • Corresponding properties

Some new semantics

= and :=

  • = maps a property to a data value or a set of data values
  • := sets a property to a specific value
pressure %>% ggvis(~temperature, ~pressure, fill = "red") %>% 
  layer_points()

= and :=

  • = maps a property to a data value or a set of data values
  • := sets a property to a specific value
pressure %>% ggvis(~temperature, ~pressure, fill := "red") %>% 
  layer_points()

= and :=

More new semantics

Objects in ggvis

ggvis code accepts three types of objects:

  • A string of letters, without quotes, refers to an object name, and will be searched in the current environment
  • A tilde ~ at the start of the string will be treated as a variable name, and will be searched in the current data frame
  • A string in quotes will be treated as a raw value.

Objects in ggvis

shade <- 'red'
mtcars %>% ggvis(~disp, ~mpg) %>% 
  layer_points(fill= ~factor(cyl), size :=300, stroke := shade, 
               strokeWidth :=6) %>% add_legend('fill',title='Cylinders')

Examples

Interplay of dplyr and ggvis

mtcars %>% group_by(am) %>% 
  ggvis(~mpg, ~hp) %>% layer_smooths(stroke = ~factor(am)) %>% 
    layer_points(fill = ~factor(am))

Interplay of dplyr and ggvis

mtcars %>% group_by(am) %>% 
  ggvis(~mpg, ~hp) %>% layer_smooths(stroke = ~factor(am)) %>% 
    layer_points(fill = ~factor(am)) %>% 
  scale_nominal('fill', range=c("green","orange")) %>% 
  scale_nominal('stroke', range=c('green','orange'))

Using derived statistics (link)

Interactive visualization

Interactive visualization

mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% 
 layer_smooths(se=T,
   span=input_slider(0.1,0.9,value=0.75,step=0.05,label='span'))

Tooltips

## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

Tooltips

mtcars %>% mutate(name=row.names(mtcars)) %>% 
  ggvis(~wt, ~mpg, key := ~name) %>% layer_points() %>% 
  add_tooltip(function(df) df$name)

Summarizing models

mtcars %>% ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_model_predictions(model = "lm", stroke:='black') %>%
  layer_model_predictions(model = "loess", stroke := "red") 

Resources

Maps (from Bob Rudis' site)